package net.steam.easyjoboa.web.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import net.steam.easyjoboa.biz.LeaveBiz;
import net.steam.easyjoboa.biz.LeaveCheckBiz;
import net.steam.easyjoboa.entity.Department;
import net.steam.easyjoboa.entity.Employee;
import net.steam.easyjoboa.entity.Leave;
import net.steam.easyjoboa.entity.LeaveCheck;
import net.steam.easyjoboa.entity.PageBean;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

public class LeaveAction {

	private LeaveBiz leaveBiz;
	private LeaveCheckBiz leaveCheckBiz;
	private Integer id;
	private List<Leave> leaves;
	private Leave leave;
	private LeaveCheck leaveCheck;
	private List<Department> departments;
	private PageBean pb;
	private String startDate;
	private String endDate;
	private String type;
	private String userName;
	private String departmentName;
	
	
	@Action(value="toLeaveDetail",results={@Result(name="success",location="/leaveDetail.jsp")})
	public String toLeaveDetail(){
		leave = leaveBiz.getLeaveById(id);
		return "success";
	}
	
	@Action(value="toMyleaves",results={@Result(name="success",location="/myLeaves.jsp")})
	public String toMyleaves(){
		Employee employee = (Employee) ServletActionContext.getRequest().getSession().getAttribute("loginUser");
		//Employee employee = employeeBiz.findEmployeeById(1);
		if(pb == null){
			pb = new PageBean();
		}
		leaves = leaveBiz.findMyLeaves(pb, employee.getUserId(), startDate, endDate);
		return "success";
	}
	
	@Action(value="doAddLeave",results={@Result(name="success",location="/leaveSuccess.jsp"),
										@Result(name="input",location="/createLeave.jsp")})
	public String doAddLeave(){
		try {
			Employee employee = (Employee) ServletActionContext.getRequest().getSession().getAttribute("loginUser");
			//Employee employee = employeeBiz.findEmployeeById(1);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			leave.setStartDate(sdf.parse(startDate));
			leave.setEndDate(sdf.parse(endDate));
			leave.setEmployee(employee);
			leave.setStatus("1");
			leaveBiz.addLeave(leave);
		} catch (Exception e) {
			e.printStackTrace();
			return "input";
		}
		return "success";
	}
	
	@Action(value="toLeavesCheck",results={@Result(name="success",location="/leaveList.jsp")})
	public String toLeavesCheck(){
		//departments = departmentBiz.getAllDepartment();
		Employee employee = (Employee) ServletActionContext.getRequest().getSession().getAttribute("loginUser");
		//Employee employee = employeeBiz.findEmployeeById(2);
		if(pb == null){
			pb = new PageBean();
		}
		leaves = leaveBiz.findAllLeaves(pb, employee.getUserId(), startDate, endDate, userName, departmentName, type);
		return "success";
	}
	
	@Action(value="toLeaveCheck",results={@Result(name="success",location="/leaveCheck.jsp")})
	public String toLeaveCheck(){
		leave = leaveBiz.getLeaveById(id);
		return "success";
	}
	
	@SuppressWarnings("unchecked")
	@Action(value="doLeaveCheck",results={@Result(name="success",location="/checkSuccess.jsp"),
			@Result(name="input",location="/leaveCheck.jsp")})
	public String doLeaveCheck(){
		try {
			//Employee employee = employeeBiz.findEmployeeById(2);
			Employee employee = (Employee) ServletActionContext.getRequest().getSession().getAttribute("loginUser");
			leaveCheck.setEmployee(employee);
			leave = leaveBiz.getLeaveById(id);
			leave.setStatus("2");
			leaveCheck.setLeave(leave);
			leaveCheck.setLcCheckDate(new Date());
			leave.getLeaveChecks().add(leaveCheck);
			leaveBiz.updateLeave(leave);
			leaveCheckBiz.addLeaveCheck(leaveCheck);
		} catch (Exception e) {
			e.printStackTrace();
			return "input";
		}
		return "success";
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public List<Leave> getLeaves() {
		return leaves;
	}

	public void setLeaves(List<Leave> leaves) {
		this.leaves = leaves;
	}

	public PageBean getPb() {
		return pb;
	}

	public void setPb(PageBean pb) {
		this.pb = pb;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public void setLeaveBiz(LeaveBiz leaveBiz) {
		this.leaveBiz = leaveBiz;
	}

	public void setLeave(Leave leave) {
		this.leave = leave;
	}

	public LeaveCheck getLeaveCheck() {
		return leaveCheck;
	}

	public void setLeaveCheck(LeaveCheck leaveCheck) {
		this.leaveCheck = leaveCheck;
	}

	public List<Department> getDepartments() {
		return departments;
	}

	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getDepartmentName() {
		return departmentName;
	}

	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}

	public Leave getLeave() {
		return leave;
	}

	public void setLeaveCheckBiz(LeaveCheckBiz leaveCheckBiz) {
		this.leaveCheckBiz = leaveCheckBiz;
	}
}
